﻿using System.Windows;
using System.ComponentModel;
using TimetableSystem.Model;
using TimetableSystem.Repository;
using System;

namespace TimetableSystem
{
    /// <summary>
    /// Contains a layout to display a LectureTimetable, Exam and Coursework dates in a listbox and a user's name/course
    /// </summary>
    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        private User _user;
        private TestRepository _repo;

        public MainWindow()
        {
            InitializeComponent();

            //Set DataContext to this page
            DataContext = this;

            _repo = new TestRepository();
            TextBoxBarcode.Focus();
        }

        public User User
        {
            get { return _user; }
            set
            {
                if (_user == value) return;

                _user = value;

                if (_user != null)
                {
                    LectureTimetable.Modules = _user.Modules;
                    LectureTimetable.Visibility = Visibility.Visible;
                }
                else
                {
                    LectureTimetable.Modules = null;
                    LectureTimetable.Visibility = Visibility.Collapsed;
                }

                OnPropertyChanged("User");                
            }
        }

        private void TextBoxBarcode_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == System.Windows.Input.Key.Enter)
            {
                User = _repo.GetUser(TextBoxBarcode.Text);
                TextBoxBarcode.Text = "";
            }
        }

        #region INotifyPropertChanged code

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion
    }
}
